﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            

        }

        protected void btnLoadData_Click(object sender, EventArgs e)
        {
          this.GridView1.DataSource= GetPersonList();
          this.GridView1.DataBind();
        }
        private List<Person> GetPagedList(int pageSize, int pageIndex, out int count)
        {
            List<Person> tmpList = GetPersonList();
            List<Person> result=new List<Person>();
            count = 0;
            if (tmpList!=null && tmpList.Count>0)
            {
                count = tmpList.Count;//记录总数
                int pageCount = tmpList.Count / pageSize; //一共有多少页
                if (tmpList.Count % pageCount>0) //每页多出规定的数量后页数要增加1
                {
                    pageCount++;
                }
                if (pageIndex<=0)//第一页
                {
                    pageIndex = 1;
                }
                if (pageIndex > pageCount) //最后一页
                {
                    pageIndex = pageCount;
                }
                for (int i = 0; i < pageSize; i++) //取记录
                {
                    int m = (pageIndex - 1) * pageSize + i;
                    if ((m >= 0) && (m < tmpList.Count))
                    {
                        result.Add(tmpList[m]);
                    }
                }
            }
            return result;
        }
        private List<Person> GetPersonList()
        {
            List<Person> list = new List<Person>();
            for (int i = 0; i < 100; i++)
            {
                Person person = new Person();
                person.Gander = true;
                person.Address = "Address" + i;
                person.Id = i + 1;
                person.Name = "Name" + new Random().Next();
                list.Add(person);
            }
            List<Person> cachedList = HttpRuntime.Cache.Get("PersonList") as List<Person>;
            if (cachedList != null)
            {
                return cachedList;
            }
            else
            {
                HttpRuntime.Cache.Insert("PersonList",list);
                return list;
            }
        }

        protected void btnGo_Click(object sender, EventArgs e)
        {
            int pageIndex = int.Parse(txtPageIndex.Text);
            int count;
            GridView1.DataSource=GetPagedList(20, pageIndex, out count);
            GridView1.DataBind();
            Response.Write(count);
        }
    }
    public class Person
    {
        private int _id;
        private bool _gander;
        private string _name;
        private string _address;

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public bool Gander
        {
            get { return _gander; }
            set { _gander = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string Address
        {
            get { return _address; }
            set { _address = value; }
        }
    }
}
